package com.xy.work.template.base.component.impl;
import cn.hutool.core.util.StrUtil;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.models.*;
import com.xy.work.template.base.component.SmsService;
import com.xy.work.template.base.utils.CheckStrUtil;
import com.xy.work.template.common.enums.CommonCodeEnum;
import com.xy.work.template.common.exception.BaseCustomException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
 * @author niuniu
 * @Auther: niuniu
 * @Date: 2021/5/14 14:51
 * @Description: 发送短信相关操作
 */
@Slf4j
@Service
public class SmsServiceImpl implements SmsService {
    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId 阿里云RAM访问ID
     * @param accessKeySecret 阿里云RAM访问密钥
     * @return Client
     * @throws Exception
     */
    @Override
    public Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        if(StrUtil.isBlank(accessKeyId)){
            throw new BaseCustomException(CommonCodeEnum.SMS_ENUM_CONFIG_ACCESS_KEY_ID);
        }
        if(StrUtil.isBlank(accessKeySecret)){
            throw new BaseCustomException(CommonCodeEnum.SMS_ENUM_CONFIG_ACCESS_KEY_SECRET);
        }
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    /**
     * 发送短信操作
     * @param accessKeyId 阿里云RAM访问ID
     * @param accessKeySecret 阿里云RAM访问密钥
     * @param phone 手机号
     * @param templateParam 模板参数，json格式 比如：{\"customer\":\"1111\"}
     * @param templateCode 发送模板code
     * @param signName 短信签名
     * @return
     */
    @Override
    public boolean send(String accessKeyId, String accessKeySecret,String phone,String templateParam,String templateCode,String signName){
        Client client=null;
        try {
            client = createClient(accessKeyId,accessKeySecret);
            if(!CheckStrUtil.isMobile(phone)){
                throw new BaseCustomException(CommonCodeEnum.PHONE_ERROR);
            }
            if(StrUtil.isBlank(templateParam)){
                throw new BaseCustomException(CommonCodeEnum.SMS_TEMPLATE_PARAM);
            }
            if(StrUtil.isBlank(templateCode)){
                throw new BaseCustomException(CommonCodeEnum.SMS_TEMPLATE_CODE);
            }
            if(StrUtil.isBlank(signName)){
                throw new BaseCustomException(CommonCodeEnum.SMS_TEMPLATE_SIGN_NAME);
            }
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(phone)
                    .setSignName(signName)
                    .setTemplateCode(templateCode)
                    .setTemplateParam(templateParam);
            SendSmsResponse response=client.sendSms(sendSmsRequest);
            log.info("向用户发《{}》短信，相应信息：{}",phone,response);
            if(response.getBody().getMessage().equals("OK")){
                return true;
            }else {
                throw new BaseCustomException(CommonCodeEnum.SMS_SEND_FAIL);
            }
        } catch (Exception e) {
            log.error("发送短信错误",e);
            return false;
        }
    }
}
